package com.py.chat.service;

import java.util.List;

import com.py.chat.bean.ChatLog;
import com.py.chat.bean.Gloable;
import com.py.chat.dao.ChatLogDao;
import com.py.chat.utils.PropertiesUtil;

public class ChatLogService {

	private static Object lock = new Object();
	
	public static void addChatLog(ChatLog chatLog){
		// 判断聊天记录是否大于50条 如果大于  移除头个信息    同时在移除数据库中移除该数据
		String maxrecord = PropertiesUtil.getValue("maxrecord");
		
		synchronized (lock) {
			if(Gloable.chatLogList.size() == Integer.valueOf(maxrecord)){
				// 先获取头数据信息
				ChatLog cLog =  Gloable.chatLogList.get(0);
				// 移除数据库数据
				removeChatLogById(cLog.getId());
				// 移除内存数据
				Gloable.chatLogList.remove(0);
				// 向内存添加数据
				Gloable.chatLogList.add(chatLog);
			}else{
				Gloable.chatLogList.add(chatLog);
			}
			// 向数据库中插入聊天记录
			chatLogToMysql(chatLog);
		}
	}
	public static void chatLogToMysql(ChatLog chatLog){
		ChatLogDao chatLogDao = new ChatLogDao();
		chatLogDao.addLog(chatLog);
	}
	public static void removeChatLogById(String id){
		ChatLogDao chatLogDao = new ChatLogDao();
		chatLogDao.removeChatLogById(id);
	}
	public static List<ChatLog> getAllList(){
		ChatLogDao chatLogDao = new ChatLogDao();
		return chatLogDao.getAllList();
	}
	
		
}
